﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Data.SqlClient;


namespace BenhVien
{
    public partial class ChangePass : Form
    {
        private Connection cons = new Connection();
        private DataTable dt = new DataTable();
        private SqlDataAdapter da = new SqlDataAdapter();
        private String userName = fmDangNhap.getUserName();
        private String passold = fmDangNhap.getPass();
        public ChangePass()
        {
            InitializeComponent();
        }

        private void btAccept_Click(object sender, EventArgs e)
        {
            if(tbPassNew.Text==tbPassRepeat.Text&&passold==tbPassOld.Text)
            {
                
                String stPass = tbPassRepeat.Text;
                DataRow row = dt.Select("Name='"+userName+"'")[0];
                row.BeginEdit();
                row["Pass"] = stPass;
                row.EndEdit();
                SqlCommand commandChangePass = new SqlCommand();
                commandChangePass.Connection = cons.con;
                commandChangePass.CommandType = CommandType.Text;
                commandChangePass.CommandText = @"Update DangNhap Set Pass = @Pass
                                                    Where Name=@Name ";
               commandChangePass.Parameters.Add("@Name", SqlDbType.NVarChar, 50, "Name");
                commandChangePass.Parameters.Add("@Pass", SqlDbType.NVarChar, 50, "Pass");
                da.UpdateCommand = commandChangePass;
                //dt.Clear();
                da.Update(dt);
                MessageBox.Show("Change Pass Success!", "MesageBox", MessageBoxButtons.OK);
                fmDangNhap.setPass(tbPassRepeat.Text);
                FormChinh _fmMain = new FormChinh();
                _fmMain.Show();
                this.Close();
               // _fmMain.Show();
            }
            else
            {
                if(MessageBox.Show("Mật khẩu lặp lại không khớp hoặc mật khẩu không đúng","Đổi mật khẩu",MessageBoxButtons.YesNo,MessageBoxIcon.Question)==DialogResult.No)
                {
                    this.Close();
                    Dispose();
                    FormChinh _fmMain = new FormChinh();
                    _fmMain.Show();
                }
            }
             
        }

        private void ChangePass_Load(object sender, EventArgs e)
        {
            this.AcceptButton = btAccept;
            cons.Connect();
            dt = fmDangNhap.getDataTable();
           
        }

        private void btExit_Click(object sender, EventArgs e)
        {
            this.Close();
            Dispose();
            FormChinh _fmMain = new FormChinh();
            _fmMain.Show();
        }
    }
}
